﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;

using Swaf.BizObj;
using Swaf.Container;

namespace Swaf.Gui.AspNet2
{
	public class EndContainingDiv : System.Web.UI.UserControl, Swaf.Gui.IInnerTemplateControl
	{
		protected int m_currentDepth = -1, m_nextDepth = -1;
		protected string m_ctlTemplate = null;
		protected List<Control> m_innerCtls;



		public void setupDepths(int initialDepth, int currentDepth, int nextDepth, Stack<IBizObj> parentStack)
		{
			m_currentDepth = currentDepth;
			m_nextDepth = nextDepth;
			if (m_ctlTemplate != null && m_currentDepth >= m_nextDepth)
			{
				IBizObj p = null;
				do
				{
					p = parentStack.Pop();
					InnerUserCtl c = new InnerUserCtl(m_ctlTemplate, p);
					c.Page = Page;
					if (m_innerCtls == null)
						m_innerCtls = new List<Control>();
					m_innerCtls.Add(c);
					c.ID = ID + m_innerCtls.Count;
				}
				while (parentStack.Count != 0 && (int)parentStack.Peek().get("depth") - initialDepth >= nextDepth);
				
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if (m_currentDepth >= m_nextDepth)
			{
				int nextInnerCtl = 0;
				int numClosingDivs = (m_currentDepth - m_nextDepth) + 1;
				for (int p = 0; p < numClosingDivs; p++)
				{
					if (p > 0)
						writer.Write("<div style='clear:both'></div>");
					if (m_innerCtls != null && m_innerCtls.Count > nextInnerCtl)
						m_innerCtls[nextInnerCtl++].RenderControl(writer);

					writer.Write("</div>");
				}
			}
		}

		public string Template { get { return m_ctlTemplate; } set { m_ctlTemplate = value; } }


		public class InnerUserCtl : BaseBizObjUserControl
		{
			protected string m_template;
			protected DictionaryStack m_macros = null;

			public InnerUserCtl(string template, IBizObj bizobj)
			{
				m_template = template;
				CurrentBizObj = bizobj;
			}

			//public IBizObj CurrentBizObj { get { return m_bizobj; } }

			protected override TextReader Html
			{
				get
				{
					return new StringReader(m_template);
				}
			}

			protected override bool createControls()
			{
				bool c = base.createControls();
				if (CurrentBizObj != null)
					for (int i = 0; i < Controls.Count; i++)
						processControl(Controls[i]);
				return c;
			}

			protected override string resolve(Control ctl, string fldName, object defaultValue)
			{
				if (m_macros == null)
					m_macros = new DictionaryStack(LocalMacros, new NameAddressSupportDictionaryWrapper(CurrentBizObj));
				return base.resolve(ctl, fldName, defaultValue);
			}

			protected override IBizObj getBizObjFromControl(Control ctl, ref string baseCtlId)
			{
				return CurrentBizObj;
			}

			protected override bool ShouldCacheStructure
			{
				get
				{
					return false;
				}
			}
		}
	}
}
